<?php

namespace org\util;
class HtmlPage {

    public $page_now; //当前页（必须）
    public $page_count; //总页数
    public $total; //数据总量（必须）
    public $page_limit; //每页显示数量（必须）
    public $page_start; //分页开始的记录数（必须）
    public $banner; //分页条显示
    public $url;
    public $kw;

    function __construct($data) {
        $this->page_now = $data['p'];
        $this->total = $data['total'];
        $this->page_limit = $data['limit'];
        $this->page_start=$data['page_start'];
        $this->page_count = ceil($this->total / $this->page_limit);
        $this->url = $data['url'];
        //$this->kw = $data['kw'];
    }

    private function first_page() {
        //判断首页
        if ($this->page_now > 1) {
            return "<li><a href='".$this->url."1"."'>首页</a></li>";
        }else{
            return "<li class=\"am-disabled\"><a href='".$this->url."1"."'>首页</a></li>";
        }
    }

    private function last_page() {
        //判断末页
        if ($this->page_now < $this->page_count) {
            return "<li><a href='".$this->url.$this->page_count."'>末页</a></li>";
        }else{
            return "<li class=\"am-disabled\"><a href='".$this->url.$this->page_count."'>末页</a></li>";
        }
    }

    private function prev() {
        //判断上一页
        if ($this->page_now > 1) {
            return "<li><a href='".$this->url.($this->page_now-1)."'><"."上一页</a></li>";
        }else{
            return "<li class=\"am-disabled\"><a href='".$this->url.($this->page_now-1)."'><"."上一页</a></li>";
        }
    }

    private function next() {
        //判断下一页
        if ($this->page_now < $this->page_count) {
            return "<li><a href='".$this->url.($this->page_now+1)."'>下一页".">"."</a></li>";
        }else{
            return "<li class=\"am-disabled\"><a href='".$this->url.($this->page_now+1)."'>下一页".">"."</a></li>";
        }
    }

    private function start() {
        //显示当前页的第一条数据行数
        if ($this->total === 0) {
            return 0;
        } else {
            return (($this->page_now - 1) * 20 + 1);
        }
    }

    private function end() {
        //显示当前页的最后一条数据行数
        return min($this->page_now * 20, $this->total);
    }

    private function ym_list() {
        /* 判断1 2 3 4 5 6.. 10显示顺序，理论：当前页为第5页，向前显示4页，向后显示5页* */
        //1、总页数小于10页，循环显示总页数
        if ($this->page_count <= 10) {
            for ($i = 1; $i <= $this->page_count; $i++) {
                if ($i==$this->page_now) {
                    $this->banner .= "<li class=\"am-active\"><a>" . $i . "</a></li>";
                }else{
                    $this->banner .= "<li><a href='" . $this->url . $i . "'>" . $i . "</a></li>";
                }
            }
            return $this->banner;
        }
        //2、当前页小于6，点击后仍显示1-10页
        elseif ($this->page_now < 6) {
            for ($i = 1; $i <= 10; $i++) {
                if ($i==$this->page_now){
                    $this->banner .= "<li class=\"am-active\"><a>" . $i . "</a></li>";
                }else{
                    $this->banner .= "<li><a href='".$this->url.$i."'>" . $i . "</a></li>";
                }
            }
            return $this->banner;
        }
        //3、当前页在最后5页内，直接显示最后的10页
        elseif ($this->page_now >= $this->page_count - 4) {
            for ($i = $this->page_count - 9; $i <= $this->page_count; $i++) {
                if ($i==$this->page_now){
                    $this->banner .= "<li class=\"am-active\"><a>" . $i . "</a></li>";
                }else {
                    $this->banner .= "<li><a href='" . $this->url . $i . "'>" . $i . "</a></li>";
                }
            }
            return $this->banner;
        }
        //4、当前页在6-（倒数5页之前）
        else {
            for ($i = $this->page_now - 4; $i <= $this->page_now + 5; $i++) {
                if ($i==$this->page_now){
                    $this->banner .= "<li class=\"am-active\"><a>" . $i . "</a></li>";
                }else {
                    $this->banner .= "<li><a href='" . $this->url . $i . "'>" . $i . "</a></li>";
                }
            }
            return $this->banner;
        }
    }

    private function mowei() {
        //显示提示信息
        $this->banner = "<span>，共有<span class='ly_1'>" . $this->total . "</span>条数据</span>";
        $this->banner .= "<span>，第<span class='ly_1'>" . $this->page_now . "/" . $this->page_count . "</span>页</span>";
        $this->banner .= "<span>，本页显示<span class='ly_1'>" . $this->start() . "-" . $this->end() . "</span>条</span>";
        return $this->banner;
    }

    function banner_list() {
            $this->banner = $this->first_page() . $this->prev() . $this->ym_list() . $this->next() . $this->last_page();
            return $this->banner;
    }
}